<template>
  <div class="app-container">
    <el-form class="searchForm" :inline="true" :model="queryForm" label-width="90px">
      <el-form-item label="姓名">
        <el-input v-model="queryForm.username"> </el-input>
      </el-form-item>
      <el-form-item label="登录账号">
        <el-input v-model="queryForm.userno"> </el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryForm.userstatus" placeholder="请选择" clearable>
          <el-option label="启用" :value="1">启用</el-option>
          <el-option label="停用" :value="0">停用</el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="info" @click="pageChange(1)">查询</el-button>
        <el-button type="warning" @click="onOAuthUpdateUser">人员同步</el-button>
        <!-- <el-button type="info" @click="onAdd">添加</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableData"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      height="500"
      stripe
      border
      style="width: 100%"
    >
      <el-table-column :resizable="false" label="操作" width="60" fixed>
        <template slot-scope="scope">
          <span class="edit-tooltip">
            <!-- <el-tooltip effect="dark" content="修改" placement="bottom">
              <el-button type="text" @click="onClickName(scope.row)">
                <svg-icon style="fill: #f46c6c" icon-class="a-xiugai"></svg-icon>
              </el-button>
            </el-tooltip> -->
            <el-tooltip effect="dark" content="分配角色" placement="bottom">
              <el-button type="text" @click="onClickroles(scope.row)">
                <svg-icon style="fill: #f46c6c" icon-class="Assignroles"></svg-icon>
              </el-button>
            </el-tooltip>
            <!-- <el-tooltip effect="dark" content="初始化密码" placement="bottom">
              <el-button type="text" :disabled="!resPasd" @click="onClickPasd(scope.row)">
                <svg-icon style="fill: #f46c6c" icon-class="Initializepassword"></svg-icon>
              </el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="修改密码" placement="bottom">
              <el-button type="text" :disabled="!resPasd" @click="onClickrevisePasd(scope.row)">
                <svg-icon style="fill: #f46c6c" icon-class="Changepassword"></svg-icon>
              </el-button>
            </el-tooltip>

         <el-tooltip effect="dark" content="审批记录" placement="bottom"> </el-tooltip> -->
          </span>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" prop="username" label="姓名" min-width="120" />
      <el-table-column :resizable="false" prop="userstatus" label="账号状态" min-width="100">
        <template slot-scope="scope">
          {{ scope.row.userstatus == '1' ? '启用' : '停用' }}
        </template>
      </el-table-column>
      <el-table-column :resizable="false" prop="userno" label="账号" min-width="120" />
      <el-table-column :resizable="false" prop="companyno" label="所属公司" min-width="120" />
      <el-table-column :resizable="false" prop="employeename" label="组织架构人员姓名" min-width="140" />
      <el-table-column :resizable="false" prop="orgname" label="所属部门名称" min-width="120" />
      <el-table-column :resizable="false" prop="curr" label="当地币别" min-width="120" />
      <el-table-column :resizable="false" prop="dutyname" label="岗位名称" min-width="120" />
    </el-table>
    <el-pagination
      style="margin: 10px"
      :current-page="queryForm.pagenum"
      :page-sizes="[20, 50, 100]"
      :page-size="queryForm.pagesize"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="sizeChange"
      @current-change="pageChange"
    />

    <el-dialog
      destroy-on-close
      :title="title"
      :visible.sync="visible"
      :close-on-click-modal="false"
      width="600px"
      @close="onClose"
    >
      <span>
        <el-form ref="formRules" :model="form" :rules="formRules" label-width="100px" :inline="false">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="form.username" />
          </el-form-item>
          <el-form-item label="账号状态" prop="userstatus">
            <el-select v-model="form.userstatus" placeholder="请选择">
              <el-option label="启用" :value="1">启用</el-option>
              <el-option label="停用" :value="0">停用</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="账号" prop="userno">
            <el-input v-model="form.userno" />
          </el-form-item>
          <el-form-item label="所属公司" prop="companyno">
            <el-input v-model="form.companyno" />
          </el-form-item>
          <el-form-item label="组织架构人员" prop="employeename">
            <el-input v-model="form.employeename" disabled>
              <el-button slot="append" type="info" icon="el-icon-zoom-in" @click="Clickdolog"></el-button>
            </el-input>
          </el-form-item>
        </el-form>
      </span>
      <div slot="footer">
        <el-button type="info" @click="onSubmit">保存</el-button>
      </div>
    </el-dialog>
    <denglurenyuandalog :visibles="visibles" @father="visibles = false" @onChoose="onChoose"></denglurenyuandalog>
    <denglurenyuanjs
      :visiblejs="visiblejs"
      :jsid="jsid"
      @fatherjs="visiblejs = false"
      @onChoosejs="onChoosejs"
    ></denglurenyuanjs>

    <denglurenyuanResPswd
      :visible-paswd="visiblePaswd"
      :paswdid="Paswdid"
      @fatherPaswd="visiblePaswd = false"
    ></denglurenyuanResPswd>
  </div>
</template>

<script>
import { QueryPageList, Save, RestorePassWord, UserRoleArr } from '@/api/settings/denglurenyuan'
import { OAuthUpdateUser } from '@/api/index.js'
import denglurenyuandalog from './denglurenyuandalog/denglurenyuandalog.vue'
import denglurenyuanjs from './denglurenyuandalog/denglurenyuanjs.vue'
import denglurenyuanResPswd from './denglurenyuandalog/denglurenyuanResPswd.vue'

export default {
  name: 'DengluRenyuan',
  components: { denglurenyuandalog, denglurenyuanjs, denglurenyuanResPswd },
  data() {
    return {
      queryForm: {
        pagenum: 1,
        pagesize: 20
      },
      total: 0,
      tableData: [],
      loading: true,
      visible: false,
      title: '登录人员',
      formRules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        userstatus: [{ required: true, message: '请选择账号状态', trigger: 'change' }],
        userno: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        employeename: [{ required: true, message: '请选择组织架构人员', trigger: 'change' }]
      },
      form: {
        userstatus: 1
      },
      jsid: '',
      Paswdid: '',
      visibles: false,
      visiblejs: false,
      visiblePaswd: false,
      userID: this.$store.getters.userInfo.id,
      resPasd: false
    }
  },
  created() {
    this.ce()
    this.onSearch()
  },
  methods: {
    async onOAuthUpdateUser() {
      this.loading = true
      try {
        const result = await OAuthUpdateUser({ userid: this.$store.getters.userInfo.id })
        if (!result.success) throw new Error(res.message)
        this.ce()
        this.onSearch()
      } catch (error) {
        console.log(error)
        Message({ message: error, type: 'warning' })
      } finally {
        this.loading = false
      }
    },
    async ce() {
      const { content } = await UserRoleArr({ userid: this.userID })
      // console.log('a', content)
      if (content != null) {
        var arr = content.roleidarr.split(',')
        this.resPasd = arr.some(item => {
          return item == 95
        })
      }
    },
    // 点击页面显示条数
    sizeChange(size) {
      this.queryForm.pagesize = size
      this.onSearch()
    },
    // 点击下一页与页码
    pageChange(page) {
      // console.log(page)
      this.queryForm.pagenum = page
      this.onSearch()
    },
    // 查询
    async onSearch() {
      this.loading = true
      const { content } = await QueryPageList(this.queryForm)
      // console.log(content)
      this.tableData = content.list
      this.total = content.total
      this.loading = false
    },
    // 添加
    onAdd() {
      // console.log(new Date())
      var time = new Date()
      this.form = { time, userstatus: 1 }

      this.visible = true
    },
    // 保存
    onSubmit() {
      this.$refs.formRules.validate(async valid => {
        if (valid) {
          const res = await Save(this.form)
          if (res.success) {
            if (this.form.id) {
              this.$message({
                type: 'success',
                message: '修改数据成功'
              })
            } else {
              this.$message({
                type: 'success',
                message: '添加数据成功'
              })
            }
            this.visible = false
            this.onSearch()
          } else {
            this.$message({
              type: 'error',
              message: res.message
            })
          }
        } else {
          return false
        }
      })
    },
    // 重置密码
    async onClickPasd(row) {
      // console.log(row)
      this.$confirm('此操作将初始到最初密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await RestorePassWord({ userid: row.id })
          if (res.success) {
            this.$message({
              type: 'success',
              message: res.message
            })
            return
          } else {
            return this.$message.error(res.message)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消重置'
          })
        })
    },
    onClickName(data) {
      this.visible = true
      // console.log(data)
      this.form = data
    },

    onClose() {},
    Clickdolog() {
      this.visibles = true
    },
    onClickroles(row) {
      this.jsid = row.id
      this.visiblejs = true
    },
    onClickrevisePasd(row) {
      // console.log(row)
      this.Paswdid = row.id
      this.visiblePaswd = true
    },

    // 获取子组件穿回来的值
    onChoose(row) {
      // console.log(row)
      this.form.employeename = row.name
      this.form.employeeid = row.id
    },
    onChoosejs(row) {
      console.log(row)
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  height: 30px;
  line-height: 30px;
  background: rgba(0, 0, 0, 0.3);
  font-size: 14px;
  text-transform: uppercase;
  margin: 0 0 5px;
  padding: 0 10px;
  display: inline-block;
  letter-spacing: 0.5px;
}
.el-select {
  width: 100% !important;
}
</style>
